$(function () {
    getPermission();
})

function editPermission(data, index) {
    layui.use(['upload', 'form', 'laydate'], function () { //此段代码必不可少
        var form = layui.form;
        var upload = layui.upload;
        var laydate = layui.laydate;
        //执行实例
        if (data == "") {
            $("#update").html("添加");
            $("#update").click(function () {
                let name = $("#name").val();
                let value = $("#value").val();
                let uri = $("#uri").val();
                let parentId = $("#parent_permission").find("option:selected").val();
                let type = $("#permission_type").find("option:selected").val();
                let icon = $("#icon").val();
                if (name == '') {
                    layer.msg("权限名不能为空", function () {
                        $("#name").focus();
                    })
                } else if (value == '') {
                    layer.msg("权值不能为空", function () {
                        $("#value").focus();
                    })
                } else if (uri == '') {
                    layer.msg("权限路径不能为空", function () {
                        $("#uri").focus();
                    })
                } else if (type == '') {
                    layer.msg("权限类型不能为空", function () {
                        $("#type").focus();
                    })
                } else {
                    // let result = myAjax("/permission/checkPermission", "get", {"username": code});
                    // if (result.data == null) {
                    let permissiondata = {
                        "name": name,
                        "value": value,
                        "uri": uri,
                        "parentId": parentId,
                        "type": type,
                        "icon": icon
                    };
                    console.log(permissiondata);
                    let result = ajaxModel("/permission/permission/addPermission", "post", permissiondata);
                    if (result.code == 200) {
                        layer.alert(result.message, {icon: 1}, function () {
                            parent.location.reload();
                            parent.layer.close(index);
                        });
                    } else
                        layer.alert(result.message, {icon: 1});
                }
            });
            $("#cancel").click(function () {
                parent.layer.close(index);
            });
        } else {
            data = JSON.parse(data);
            $("#name").val(data.name);
            $("#value").val(data.value);
            $("#uri").val(data.uri);
            $("#parent_permission").find("option[value='" + data.parentId + "']").attr("selected", true);
            $("#permission_type").find("option[value='" + data.type + "']").attr("selected", true);
            $("#icon").val(data.icon);
            $("#update").click(function () {
                let name = $("#name").val();
                let value = $("#value").val();
                let uri = $("#uri").val();
                let parentId = $("#parent_permission").find("option:selected").val();
                let type = $("#permission_type").find("option:selected").val();
                let icon = $("#icon").val();
                if (name == '') {
                    layer.msg("权限名不能为空", function () {
                        $("#name").focus();
                    })
                } else if (value == '') {
                    layer.msg("权值不能为空", function () {
                        $("#value").focus();
                    })
                } else if (uri == '') {
                    layer.msg("权限路径不能为空", function () {
                        $("#uri").focus();
                    })
                } else if (type == '') {
                    layer.msg("权限类型不能为空", function () {
                        $("#type").focus();
                    })
                } else {
                    // let result = myAjax("/permission/checkPermission", "get", {"username": code});
                    // if (result.data == null) {
                    let permissiondata = {
                        "id": data.id,
                        "name": name,
                        "value": value,
                        "uri": uri,
                        "parentId": parentId,
                        "type": type,
                        "icon": icon
                    };
                    console.log(permissiondata);
                    let result = ajaxModel("/permission/permission/updatePermission", "post", permissiondata);
                    if (result.code == 200) {
                        layer.alert(result.message, {icon: 1}, function () {
                            parent.location.reload();
                            parent.layer.close(index);
                        });
                    } else
                        layer.alert(result.message, {icon: 1});
                }
            });
            $("#cancel").click(function () {
                parent.layer.close(index);
            });
        }
        form.render();
    });
}

//获取码表数据
function getPermission() {
    let data = null;
    if (sessionStorage.getItem("permission") != null) {
        data = JSON.parse(sessionStorage.getItem("permission"));
    } else {
        let result = ajaxModel("/permission/permission/getPermissions", "get");
        data = result.data;
        sessionStorage.setItem("permission", JSON.stringify(data));
    }
    console.log(data);
    $("#parent_permission").html("<option value=0>暂不选择</option>");
    $.each(data, function (i, permission) {
        $("#parent_permission").append("<option value='" + permission.id + "'>" + permission.name + "</option>");
    });
}
